local liance = fk.CreateSkill{
    name = "wk_heg__liance",
}

Fk:loadTranslationTable{
    ["wk_heg__liance"] = "连策",
    [":wk_heg__liance"] = "其他角色的出牌阶段结束时，若其于此阶段内使用过同名普通锦囊牌，你可弃置一张牌，令其选择是否视为使用此回合内其使用过的其中一张同名普通锦囊牌。",

    ["#wk_heg__liance-invoke"] = "连策：你可以弃置一张牌，令 %dest 视为使用锦囊牌",
    ["#wk_heg__liance-choose"] = "连策：选择你要使用的牌",

    ["$wk_heg__liance1"] = "将军今出洛阳，恐难再回。",
    ["$wk_heg__liance2"] = "贼示弱于外，必包藏祸心。",
}

liance:addEffect(fk.EventPhaseEnd,{
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
  if not (player:hasSkill(liance.name) and target.phase == Player.Play and target ~= player and not player:isNude()) then return false end
    local usedCardNames = {}
    return #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
    local use = e.data
    if table.contains(usedCardNames, e.data.card.name) then
      return use.from == target and use.card:isCommonTrick()
      else
      if use.from == target and use.card:isCommonTrick() then
          table.insert(usedCardNames, e.data.card.name)
          end
          return false
      end
    end, Player.HistoryPhase) > 0
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askToDiscard(player,{
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = liance.name,
        prompt = "#wk_heg__liance-invoke::"..target.id,
        skip = true,
        cancelable = true,
    })
    if #cards == 1 then
      event:setCostData(self,{ cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local usedCardNames = {}
    room:throwCard(event:getCostData(self).cards[1], liance.name, player, player)
    local events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 998, function(e)
      local use = e.data
      if table.contains(usedCardNames, e.data.card.name) then
        return use.from == target and use.card.type == Card.TypeTrick
      else
        if use.from == target and use.card.type == Card.TypeTrick then
          table.insert(usedCardNames, e.data.card.name)
        end
        return false
      end
    end, Player.HistoryPhase)
    local usedCardTwice = {}
    table.forEach(events, function(e)
      table.insertIfNeed(usedCardTwice, e.data.card.name)
    end)
    room:setPlayerMark(target, "wk_heg__liance-phase", usedCardTwice)
    local success, dat = player.room:askToUseActiveSkill(target, {
        skill_name = "#wk_heg__liance_viewas",
        prompt = "#wk_heg__liance-choose",
        cancelable = true,
    })
    if success and dat then
      local card = Fk.skills["#wk_heg__liance_viewas"]:viewAs(usedCardTwice.cards)
      room:useCard{
          from = target,
          tos = dat.targets,
          card = card,
      }
    end
  end,
})

return liance